package com.example.lenovo.a203_yhy_ordering_3.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodListAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.FoodListBean;
import com.example.lenovo.a203_yhy_ordering_3.beans.SearchBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.FoodListListener;
import com.example.lenovo.a203_yhy_ordering_3.listener.SearchListener;
import com.example.lenovo.a203_yhy_ordering_3.model.SearchModel;

import java.util.List;

public class SearchResultActivity extends AppCompatActivity {
    private RecyclerView recyclerview;
    private List<FoodListBean> foodListBeans;
    private FoodListAdapter foodListAdapter;
    private String search;
    private LinearLayoutManager layoutManager;//显示布局效果
    private ImageView imageView8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        imageView8=findViewById(R.id.imageView8);
        search = getIntent().getStringExtra("search");

        initRecyclerView();
        initEnents();

        //菜谱列表
        SearchModel searchModel=new SearchModel();
        searchModel.getSearchResult(search,searchListener);
    }

    SearchListener searchListener = new SearchListener() {
        @Override
        public void onResponse(List<FoodListBean> foodListBean) {
            foodListBeans = foodListBean;
            foodListAdapter.setList(foodListBeans);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(SearchResultActivity.this, "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void initRecyclerView() {
        recyclerview=findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerview.setHasFixedSize(true);
        //实例化适配器
        foodListAdapter=new FoodListAdapter();
        FoodListAdapter.OnItemClickListener onItemClickListener= new FoodListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent= new Intent(SearchResultActivity.this, FoodInfoActivity.class);
                intent.putExtra("food_id",foodListBeans.get(position).getFood_id());
                intent.putExtra("price",foodListBeans.get(position).getPrice());
                intent.putExtra("foodname",foodListBeans.get(position).getFoodname());
                intent.putExtra("intro",foodListBeans.get(position).getIntro());
                intent.putExtra("pic",foodListBeans.get(position).getPic());
                startActivity(intent);
                Toast.makeText(SearchResultActivity.this, "click position:"+position, Toast.LENGTH_SHORT).show();
            }
        };
        foodListAdapter.setOnItemClickListener(onItemClickListener);
        recyclerview.setAdapter(foodListAdapter);
    }

    public void initEnents(){
        imageView8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
